package com.shwread.android.qysw.activity.healthy;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shwread.android.qysw.step.view.PickerView;
import com.shwread.android.qysw.tools.TimeHelper;
import com.shwread.android.qysw10000182.R;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Fantasy on 16/4/2.
 */
public class GroupPKCreateActivity extends Activity {
    @InjectView(R.id.back_btn)
    ImageView backBtn;
    @InjectView(R.id.back_text)
    TextView backText;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.right_text)
    TextView rightText;
    @InjectView(R.id.right_img)
    ImageView rightImg;
    @InjectView(R.id.group_pk_name_text)
    EditText groupPkNameText;
    @InjectView(R.id.group_pk_name_layout)
    LinearLayout groupPkNameLayout;
    @InjectView(R.id.start_date)
    TextView startDate;
    @InjectView(R.id.start_date_button)
    LinearLayout startDateButton;
    @InjectView(R.id.end_date)
    TextView endDate;
    @InjectView(R.id.end_date_button)
    LinearLayout endDateButton;
    @InjectView(R.id.group_explain)
    EditText groupExplain;
    @InjectView(R.id.every_day_goal_step_button)
    LinearLayout everyDayGoalStepButton;
    @InjectView(R.id.choose_friends_button)
    RelativeLayout chooseFriendsButton;
    @InjectView(R.id.daily_step_goal_text)
    TextView dailyStepGoalText;
    @InjectView(R.id.group_daily_step_layout)
    LinearLayout groupDailyStepLayout;
    @InjectView(R.id.cancle)
    TextView cancle;
    @InjectView(R.id.sure)
    TextView sure;
    @InjectView(R.id.line_view)
    View lineView;
    @InjectView(R.id.item_pv)
    PickerView itemPv;
    @InjectView(R.id.picker_layout)
    RelativeLayout pickerLayout;
    @InjectView(R.id.game_name)
    TextView gameName;
    @InjectView(R.id.game_sm)
    TextView gameSm;

    public static GroupPKCreateActivity groupPKCreateActivity;
    private DatePickerDialog startTimeDialog;

    private long startTime = 0;
    private long endTime = 0;
    private int dayCount = 0;
    public Calendar startCalendar;
    public Calendar endCalendar;

    private DatePickerDialog endTimeDialog;
    private String selectText;

    private String mTargetStart;
    private String mTargetEnd;


    public String mMatchException = "";//比赛说明
    public String mMatchName = "";//比赛名称
    public String mPlayers = "";//1,2,3

    private int isNeedStep = 0;//0不需要,1需要

    private String dailyGoalStep;
    String[] step = {"2000", "5000", "8000", "10000", "13000", "15000", "18000", "20000", "25000", "30000"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.h_group_pk_create);
        ButterKnife.inject(this);
        groupPKCreateActivity=this;
        Intent intent = getIntent();
        isNeedStep = intent.getIntExtra("isNeedStep", 0);
        if (isNeedStep == 1) {
            gameName.setText("竞赛名称");
            gameSm.setText("竞赛说明");
            titleText.setText("目标竞赛");
            groupDailyStepLayout.setVisibility(View.VISIBLE);
            groupDailyStepLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showMyPickerView(step);
                }
            });
        } else {
            gameName.setText("团赛名称");
            gameSm.setText("团赛说明");
            titleText.setText("创建团赛");
            groupDailyStepLayout.setVisibility(View.GONE);
        }

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        startDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimeDialog.show();
            }
        });
        endDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                endTimeDialog.show();
            }
        });


        Calendar calendar = Calendar.getInstance(Locale.CHINA);

        startTimeDialog = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                startDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                try {
                    if (TimeHelper.isBerfore12Clock()) {
                        startTime = TimeHelper.getTodayStartTime(startDate.getText().toString());
                    } else {
                        startTime = TimeHelper.getStartTime(startDate.getText().toString());
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                mTargetStart = startTime + "";

            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

        endTimeDialog = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                endDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                try {
                    endTime = TimeHelper.getEndTime(endDate.getText().toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                mTargetEnd = endTime + "";

            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//        if (TimeHelper.isBerfore12Clock()) {
//            startTimeDialog.getDatePicker().setMinDate(TimeHelper.getTodayTime());
//            endTimeDialog.getDatePicker().setMinDate(TimeHelper.getTodayTime());
//        } else {
        startTimeDialog.getDatePicker().setMinDate(TimeHelper.getTodayTime() + 24 * 60 * 60 * 1000);
        endTimeDialog.getDatePicker().setMinDate(TimeHelper.getTodayTime() + 24 * 60 * 60 * 1000);
//        }


        chooseFriendsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isComplete()) {
                    if (endTime < startTime) {
                        Toast.makeText(GroupPKCreateActivity.this, "结束时间不能小于开始时间", Toast.LENGTH_LONG).show();
                    } else if ((endTime - startTime) / (1000 * 60 * 60 * 24) > 365) {
                        Toast.makeText(GroupPKCreateActivity.this, "比赛起止日期不超过1年", Toast.LENGTH_LONG).show();
                    } else {
                        Intent intent = new Intent(GroupPKCreateActivity.this, HealthyChooseFriendActivity.class);
                        if (isNeedStep == 1) {
                            dailyGoalStep = selectText;
                            intent.putExtra("gameType", "3");
                        } else {
                            intent.putExtra("gameType", "2");
                        }

                        intent.putExtra("matchName", mMatchName);
                        intent.putExtra("matchException", mMatchException);
                        intent.putExtra("startTime", mTargetStart);
                        intent.putExtra("endTime", mTargetEnd);
                        intent.putExtra("targetStep", dailyGoalStep);
                        startActivity(intent);
                    }
                } else {
                    Toast.makeText(GroupPKCreateActivity.this, "请完善信息", Toast.LENGTH_LONG).show();
                }

            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickerLayout.setVisibility(View.GONE);
                for (int i = 0; i < step.length; i++) {
                    if (selectText.equals(step[i])) {
                        int j = i + 1;
                        dailyStepGoalText.setText(selectText + "完成目标竞赛后可获得" + j + "个积分");
                    }
                }

            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickerLayout.setVisibility(View.GONE);
            }
        });
    }

    private boolean isComplete() {
        boolean isComplete = false;
        mMatchName = groupPkNameText.getText().toString();
        mMatchException = groupExplain.getText().toString();
        if (TextUtils.isEmpty(mMatchName) || TextUtils.isEmpty(mTargetStart) || TextUtils.isEmpty(mTargetEnd) || TextUtils.isEmpty(mMatchException)) {
            return false;
        } else {
            return true;
        }
    }

    public void showMyPickerView(final String[] strings) {
        pickerLayout.setVisibility(View.VISIBLE);
        List<String> data = new ArrayList<String>();
        for (int i = 0; i < strings.length; i++) {
            data.add(strings[i]);
        }
        itemPv.setData(data);
        selectText = data.get(0);
        itemPv.setOnSelectListener(new PickerView.onSelectListener() {

            @Override
            public void onSelect(String text) {
                selectText = text;
            }
        });
    }
}
